#include "um_log.h"

#if defined(UM_LITE_LOG_ENABLED)
static log_client logcb;

static char *lvl_names[] = {
    "emg", "crt", "err", "wrn", "inf", "dbg",
};

void LITE_syslog_routine(const char *f, const int l, const int level, const char *fmt, va_list* params)
{
    char       *tmpbuf = logcb.text_buf;
    char       *o = tmpbuf;
    int         truncated = 0;

    if (!strlen(LITE_get_logname()) || LITE_get_loglevel() < level || level < LOG_EMERG_LEVEL) {
        return;
    }

    LITE_printf(LOG_PREFIX_FMT, lvl_names[level], f, l);

    memset(tmpbuf, 0, sizeof(logcb.text_buf));

    o = tmpbuf;
    o += LITE_vsnprintf(o, LOG_MSG_MAXLEN + 1, fmt, *params);

    if (o - tmpbuf > LOG_MSG_MAXLEN) {
        o = tmpbuf + strlen(tmpbuf);
        truncated = 1;
    }
    if (strlen(tmpbuf) == LOG_MSG_MAXLEN) {
        truncated = 1;
    }

    LITE_printf("%s", tmpbuf);
    if (truncated) {
        LITE_printf(" ...");
    }

    LITE_printf("\r\n");
    return;
}

void LITE_syslog(const char *f, const int l, const int level, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    LITE_syslog_routine(f, l, level, fmt, &ap);
    va_end(ap);
}

int log_multi_line_internal(const char *f, const int l,
                            const char *title, int level, char *payload, const char *mark)
{
    const char     *pos;
    const char     *endl;
    int             i;

    if (LITE_get_loglevel() < level) {
        return 1;
    }

    LITE_printf("[%s] %s(%d): %s (Length: %d Bytes)\r\n",
                lvl_names[LITE_get_loglevel()], f, l, title, (int)strlen(payload));

    pos = payload;
    while (pos && *pos) {
        LITE_printf("%s ", mark);

        if (*pos == '\r') {
            LITE_printf("\r\n");
            pos += 2;
            continue;
        }

        endl = strchr(pos + 1, '\r');
        if (endl == NULL) {
            endl = pos;
            do {
                ++endl;
            } while (*endl);
        }

        for (i = 0; i < endl - pos; ++i) {
            LITE_printf("%c", pos[i]);
        }
        LITE_printf("\r\n");

        pos = *endl ? endl + 2 : 0;
    }

    return 0;
}

#else   /* defined(LITE_LOG_ENABLED) */
//void LITE_syslog(const char *f, const int l, const int level, const char *fmt, ...)
//{
//    va_list         ap;
//
//    HAL_Printf("%s/%d: ", f, l);
//    va_start(ap, fmt);
//    vprintf(fmt, ap);
//    va_end(ap);
//    HAL_Printf("\r\n");
//
//    return;
//}
//
//int log_multi_line_internal(const char *f, const int l,
//                            const char *title,
//                            int level,
//                            char *payload,
//                            const char *mark)
//{
//    HAL_Printf("%s(%d):\r\n\r\n", f, l);
//    HAL_Printf("%s\r\n", payload);
//
//    return 0;
//}

//char *LITE_get_logname(void)
//{
//    return logcb.name;
//}
//
//int LITE_get_loglevel(void)
//{
//    return logcb.priority;
//}
//
//void LITE_set_loglevel(int pri)
//{
//    logcb.priority = pri;
//}

//void LITE_rich_hexdump(const char *f, const int l,
//                       const int level,
//                       const char *buf_str,
//                       const void *buf_ptr,
//                       const int buf_len)
//{
//    if (LITE_get_loglevel() < level) {
//        return;
//    }
//
//    LITE_printf(LOG_PREFIX_FMT, lvl_names[LITE_get_loglevel()], f, l);
//    LITE_printf("HEXDUMP %s @ %p[%d]\r\n", buf_str, buf_ptr, buf_len);
//    LITE_hexdump(buf_str, buf_ptr, buf_len);

//    return;
//}


#endif


//    memset(tmpbuf, 0, sizeof(logcb.text_buf));
//
//    o = tmpbuf;
//    o += LITE_vsnprintf(o, LOG_MSG_MAXLEN + 1, fmt, *params);
//
//    if (o - tmpbuf > LOG_MSG_MAXLEN) {
//        o = tmpbuf + strlen(tmpbuf);
//        truncated = 1;
//    }
//    if (strlen(tmpbuf) == LOG_MSG_MAXLEN) {
//        truncated = 1;
//    }
//
//    LITE_printf("%s", tmpbuf);
//    if (truncated) {
//        LITE_printf(" ...");
//    }
//
//    LITE_printf("\r\n");
//    return;

